package com.cyy.system.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cyy.common.domain.Response;
import com.cyy.common.pojo.dto.CategoryFormDto;
import com.cyy.common.pojo.dto.DictTypeFormDto;
import com.cyy.common.pojo.dto.common.OptionDto;
import com.cyy.common.pojo.query.system.SystemCategoryQuery;
import com.cyy.common.pojo.vo.system.SystemCategoryVo;
import com.cyy.common.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author caoyunyou
 */
@Tag(name = "分类")
@RestController
@RequestMapping("/system/category")
@RequiredArgsConstructor
@SaCheckLogin
public class CategoryController {

    private final CategoryService categoryService;


    @GetMapping("/page")
    public Response getPage(SystemCategoryQuery query) {
        IPage<SystemCategoryVo> sysVo = categoryService.getPage(query);
        return Response.success().data(sysVo);
    }

    @Operation(summary = "下拉框")
    @GetMapping("/options")
    public Response getOptions() {
        List<OptionDto<String, Integer>> res = categoryService.getOptions();
        return Response.success().data(res);
    }

    @GetMapping("/{id}/form")
    public Response getCategoryForm(@PathVariable("id") Integer id) {
        CategoryFormDto res = categoryService.getCategoryFormById(id);
        return Response.success().data(res);
    }

    @PostMapping("")
    public Response addCategory(@RequestBody CategoryFormDto req) {
        categoryService.addCategory(req);
        return Response.success();
    }

    @PutMapping("")
    public Response updateCategory(@RequestBody CategoryFormDto req) {
        categoryService.updateCategory(req);
        return Response.success();
    }

    @DeleteMapping("/{ids}")
    public Response deleteCategories(@PathVariable("ids") String ids) {
        categoryService.deleteCategories(ids);
        return Response.success();
    }
}
